<?php
class WeixinManager
{
	public static function info ($user)
	{
        if ($user->get_role_id() != 'supermanager')
            return "没有权限";

		$name = $user->get_user_name();
		$str = "方寸掌悦管理员：$name";
		return $str;
	}

    public static function bjmobile_list($user)
    {
        if ($user->get_role_id() != 'supermanager')
            return "没有权限";

        $list = DB::select('c.machine', 'c.uid', 'c.ctime', 'nickname', 'u.passport')
            ->from(array('user_basic', 'u'))
            ->join(array('oct_data_config', 'c'), 'LEFT')
            ->on('u.uid', '=', 'c.uid')
            ->where('u.utype', '=', 'merchant')
            ->where('c.machine', 'like', 'bjm%')
            ->order_by('c.ctime', 'asc')
            ->execute();
        

        $strs  = array();
        $count = 0;
        foreach ($list as $rs)
        {
            $count += 1;
            $name = $rs['nickname'];
            $passport = $rs['passport'];
            $strs[] = sprintf("%03d %s\n%03d %s\n", $count, $passport, $count, $name);
        }

		return "北京移动直营厅($count):\n------------------\n".implode("\n", $strs);
    }

	public static function cpa_data ($user)
	{
        if ($user->get_role_id() != 'supermanager')
            return "没有权限";

		$day = DB::select('count_time')
			->from('oct_cpa_data')
			->order_by('count_time', 'DESC')
			->limit(1)
			->execute()
			->get('count_time');

		$query = DB::select()
			->from('oct_cpa_data')
			->where('count_time', '=', $day)
			->execute();

		$strs = array();
		foreach ($query as $rs)
		{
			$name = $rs['name'];
			$count = $rs['count'];
			$day   = $rs['count_time'];
			$strs[] = "$name\t$count";
		}

		return "CPA: $day:\n------------------\n".implode("\n", $strs);
	}

	public static function data ($user)
	{
		
		$query = DB::select('m.*', 'u.comments')
			->from (array('oct_data_stats', 'm'))
			->join(array('user_basic', 'u'), 'LEFT')
			->on('m.uid', '=', 'u.uid')
			->order_by('m.day', 'DESC')
			->limit(5)
			->execute();

		//Kohana::$log->add(Log::DEBUG, print_r($query, TRUE));
		$strs = array();
		foreach ($query as $rs)
		{
			$brands = $rs['brand'];
			$mobiles= $rs['imei'];
			$day    = $rs['day'];
			$name   = $rs['comments'];

			$strs[] = "[$name] \n$day, $mobiles";
		}

		return implode("\n", $strs);
	}
}
